package cn.edu.swu.bc.servlet;
import cn.edu.swu.bc.utils.DBUtils;
import cn.edu.swu.bc.utils.FileUtils;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


public class DeleteBlogServlet extends HttpServlet {

    private static final long serialVersionUID = 1121212548834340L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        FileUtils.deleteBlogContent(id, request);
        FileUtils.deleteBlogPic(id, request);
        String sql1 = "delete from `users-blogs` where bid=" + id;
        String sql2 = "delete from `blogs` where id=" + id;
        try {
            DBUtils.delete(sql1);
            DBUtils.delete(sql2);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        response.sendRedirect("./index.html");
    }
}
